<?php

namespace blog\admin\controller;

use think\Controller;
use think\Db;

class Index extends Controller
{
    public function _initialize()
    {
        check_login();
    }

    private function breadCrumb()
    {
        $html = '<li><a href="'.url('Index/index').'">后台首页</a></li>';
        $html .= '<li>'.html_link(url('Type/datalist', "page=1&pid=0"), '#datalist', 'a', '类型管理').'</li>';
        $html .= '<li>';
        $root = sqlAll('select id,name from bg_type where pid=0');
        foreach ($root as $row) {
            $html .= html_link(url('Type/datalist', 'page=1&pid='.$row['id'].'&name='.$row['name']), '#datalist', 'a', $row['name']).'&emsp;';
        }
        $html .= '</li>';
        // return $html.url('index/index', ['page' => 1, 'pid' => 0]);
        return $html;
    }

    public function index()
    {
        // dump('<div style="height=120px"></div>');
        // dump(\think\Session::get());
        // dump(sqlCol('select * from bg_type; select * from bg_user'));
        $data = (new Type)->datalist(0);

        if ($this->request->isAjax())
            return $data;

        $this->assign('title', "管理首页");
        $this->assign('breadcrumb', $this->breadCrumb());
        $this->assign('data', $data);
        return $this->fetch();
    }

    public function data()
    {
        $list = \DataList::obj('bg_type', 'datalist')
                    ->where('pid=0')->fetch(1, 5);

        if ($this->request->isAjax()) {
            $this->assign('ajax', true);
            $this->assign('list', $list);
            return json_return_data($this->fetch());
        }


        $this->assign('close_bread');
        $this->assign('list', $list);
        return $this->fetch();
    }

    public function password()
    {
        if ($this->request->isPost()) {
            if ($_POST['verify'] !== $_POST['pwd'])
                return json_return('两次输入的密码不一致!');

            $pwd = md5(md5($_POST['pwd']));

            if (isset($_POST['uid']) && $_POST['uid'] != '') {
                $ret = Db::table('bg_user')
                        ->where('id', $_POST['uid'])
                        ->update([
                            'name' => $_POST['name'],
                            'password' => $pwd
                       ]);

            } else {
                $ret = Db::table('bg_user')->insert([
                    'name' => $_POST['name'],
                    'phone' => "13500221122",
                    'email' => 'admin@163.com',
                    'password' => $pwd
                ]);
            }

            if (!$ret)
                return json_return('操作失败!');

            return json_return([
                'close' => true,
                'msg' => '操作成功!'
            ]);
        }
        $row = sqlRow("select id, name, password from bg_user where role=0");
        if (count($row) == 0)
            $row = ['id' => '', 'name' => '', 'password' => ''];

        $this->assign('row', $row);
        return view();
    }
}

